#include "IDGGSKernel.hpp"
#include "ISEA4H/ISEA4HImpl.hpp"
#include "ISEA4T/ISEA4TImpl.hpp"
#include "ISEA4D/ISEA4DImpl.hpp"
#include "QueryResult.hpp"

using namespace NNU::DGGS::Base;
using namespace NNU::DGGS::Kernel;
using namespace NNU::DGGS::Kernel::ISEA4H;
using namespace NNU::DGGS::Kernel::ISEA4T;
using namespace NNU::DGGS::Kernel::ISEA4D;

IDGGSKernel* pKernel = nullptr;

extern "C" DGGS_KERNEL_API IDGGSKernel * CreateKernelObj(DGGSGridType gridType)
{
	if (pKernel != nullptr)
	{
		pKernel = nullptr;
	}
	switch (gridType)
	{
	case DGGSGridType::DGGS_ISEA4H:
		pKernel = new ISEA4HImpl();
		break;
	case DGGSGridType::DGGS_ISEA4T:
		pKernel = new ISEA4TImpl();
		break;
    case DGGSGridType::DGGS_ISEA4D:
        pKernel = new ISEA4DImpl();
        break;
	default:
		break;
	}

    if(pKernel != nullptr)
    {
        pKernel->init();
    }

	return pKernel;
}

extern "C" DGGS_KERNEL_API void DestroyKernelObj(IDGGSKernel * kernel)
{
	if (kernel != nullptr)
	{
        kernel->release();
	}
	delete kernel;
    kernel = nullptr;
}

bool IDGGSKernel::query(DGGSTopoType queryType, DGGSElementCode code, QueryResult& result)
{
    DGGSElementCodeIter*iter= nullptr;
    int codeCount=0;
    this->query(queryType,code,iter,codeCount);
	result.setData(iter, codeCount);
    delete[]iter;
    return true;
}

